import Api from 'Api/index';
import appConfig from 'AppConfig/index';
import * as common from "Commons/index";
import globalBehavior from 'Behaviors/global';
import { uploadFiles } from 'Utils/uploadFile';
Page({
    behaviors: [globalBehavior],
    data: {
        pageShow: '',
        userInfo: {},
        queryMainShow: false,
        invPop: false,
        quickList: [
			{ key: 'integral', name: '评价', icon: 'star-o', show: true, needLogin: true, url: '' },
			{
                key: '更多玩法',
                name: '更多玩法',
                icon: 'coupon-o',
                show: true,
                needLogin: true,
                url: '/package/pages-caipiao/home/home',
            },
        ],
    },
    async onLoad(opt) {
		this.getUserInfo();
	},
	getUserInfo(){
		Api.userinfo().then(res=>{
			if( res.success ){
				res = res.result||{};
				if( res.isAdmin ){
					this.data.quickList[1].show = true;
					this.setData({
						quickList: this.data.quickList,
					});
				}
				this.setData({
					userInfo:res
				})
			}
		})
	},

    quickItemClick(event) {
        const { index } = event.currentTarget.dataset;
        const item = this.data.quickList[index];
        if (item && item.name == '评价') {
            this.wxacommentplugin();
        } else {
            wx.navigateTo({
                url: item.url,
            });
        }
    },
    wxacommentplugin() {
        var plugin = requirePlugin('wxacommentplugin');
        console.log(plugin);
        plugin.openComment({
            wx_pay_id: '',
            success: (res) => {
                console.log('plugin.openTradeComment success', res);
            },
            fail: (res) => {
                console.log('plugin.openTradeComment fail', res);
            },
        });
	},
	// 用户信息
	getUserProfile(e){
		wx.getUserProfile({
			desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			success: async (res) => {
				await Api.getUserProfile(res);
				this.getUserInfo()
			},
		})
	},
	// 编辑用户头像
    onChooseAvatar(e) {
		const { avatarUrl } = e.detail;
		this.setUpdateUseravatar(avatarUrl)
	},
	// 编辑用户昵称
	chnageNickname(e){
		let { value } = e.detail;
		Api.userinfo({  nickName:value }).then(res=>{
			if( res.success ){
				this.setData({
					userInfo:res
				})
			}
		})
	},
	async setUpdateUseravatar(avatarUrl){
		var images = await uploadFiles([avatarUrl], 'avatar')
		Api.userinfo({  avatarUrl:images[0].url }).then(res=>{
			if( res.success ){
				this.setData({
					userInfo:res
				})
			}
		})
	},
    onShow: function () {},
    onShareAppMessage(res) {
        let shareObj = {
            title: '从未放弃，无所畏惧的你！',
            imageUrl:
                'https://6c7a-lzhsus-1g4h29bs69c66542-1301447037.tcb.qcloud.la/share-icon.png?sign=a3405bc98afd3bbda9c76d72ee6571e9&t=1612153450',
            path: '/pages/my/my?scene=onshare',
        };
        return shareObj;
    },
});
